﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Ryder_US_Website.BUS
{
    public class CargoBUS
    {
        //public static Breakers_RyderUSDataContext context =
        //    new Breakers_RyderUSDataContext(ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString);
        public static string conString = ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString;
        #region Select
        ///<summary>
        /// Gets all cargos
        ///</summary>
        ///<param name="curPage"> The current page.<.param>
        ///<param name="rowPerPage"> The number of rows. Rows per page</param>
        ///<param name="numPage"> The number of page.</param>
        ///<returns></returns>
        public static List<RUS_Cargo> GetAll(int curPage, int rowPerPage, ref int numPage)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowPerPage < 1)
                rowPerPage = 10;
            List<RUS_Cargo> list = new List<RUS_Cargo>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            orderby c.Cargo_sendDate
                            select c;
                numPage = (query.Count() / rowPerPage) + 1;
                list = query.Skip((curPage - 1) * rowPerPage).Take(rowPerPage).ToList<RUS_Cargo>();
            }
            return list;
        }

        ///<summary>
        /// Gets all.
        ///</summary>
        ///<returns></returns>
        public static List<RUS_Cargo> GetAll()
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                List<RUS_Cargo> list = new List<RUS_Cargo>();
                var query = from c in context.RUS_Cargos
                            orderby c.Cargo_sendDate
                            select c;
                list = query.ToList<RUS_Cargo>();
                return list;
            }
        }

        ///<summary>
        /// Get the active cargos
        ///</summary>
        ///<param name="currPage"> The current page</param>
        ///<param name="rowsPerPage"> The number of rows. Rows per page</param>
        ///<param name="numPages"> The number of pages.</param>
        ///<returns></returns>
        public static List<RUS_Cargo> GetActiveCargos(int curPage, int rowsPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowsPerPage < 1)
                rowsPerPage = 10;
            List<RUS_Cargo> list = new List<RUS_Cargo>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_trash == 0)
                            orderby c.Cargo_sendDate
                            select c;
                numPages = (query.Count() / rowsPerPage) + 1;
                list = query.Skip((curPage - 1) * rowsPerPage).Take(rowsPerPage).ToList<RUS_Cargo>();
            }
            return list;
        }

        ///<summary>
        /// Gets the active cargos
        ///</summary>
        ///<returns></returns>
        public static List<RUS_Cargo> GetActiveCargos()
        {
            List<RUS_Cargo> list = new List<RUS_Cargo>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_trash == 0)
                            orderby c.Cargo_sendDate
                            select c;
                list = query.ToList<RUS_Cargo>();
            }
            return list;
        }

        ///<summary>
        /// Gets the inactive cargos
        ///</summary>
        ///<param name="curPage">The current page.</param>
        ///<param name="rowsPerPage">The number of rows. Rows per page.</param>
        ///<param name="numPages">The number of pages.</param>
        ///<returns></returns>
        public static List<RUS_Cargo> GetInactiveCargos(int curPage, int rowPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowPerPage < 1)
                rowPerPage = 10;
            List<RUS_Cargo> list = new List<RUS_Cargo>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_trash == 1)
                            orderby c.Cargo_sendDate
                            select c;
                numPages = (query.Count() / rowPerPage) + 1;
                list = query.Skip((curPage - 1) * rowPerPage).Take(rowPerPage).ToList<RUS_Cargo>();
            }
            return list;
        }

        ///<summary>
        /// Gets the inactive cargos
        ///</summary>
        ///<returns></returns>
        public static List<RUS_Cargo> GetInactiveCargos()
        {
            List<RUS_Cargo> list = new List<RUS_Cargo>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_trash == 1)
                            orderby c.Cargo_sendDate
                            select c;
                list = query.ToList<RUS_Cargo>();
            }
            return list;
        }

        ///<summary>
        ///Gets the cargo by ID
        ///</summary>
        ///<param name="id"> The id.</param>
        ///<returns></returns>
        public static RUS_Cargo GetCargoByID(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_ID == id)
                            select c;
                return query.SingleOrDefault<RUS_Cargo>();
            }
        }

        ///<summary>
        /// Gets th active cargo by id
        ///</summary>
        ///<param name="id"> The id.</param>
        ///<returns></returns>
        public static RUS_Cargo GetActiveCargoByID(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_ID == id && c.Cargo_trash == 0)
                            select c;
                return query.SingleOrDefault<RUS_Cargo>();
            }
        }
        #endregion

        #region Insert
        ///<summary>
        /// Inserts the cargo
        ///</summary>
        ///<param name="cargo"> The carog.</param>
        public static void Insert(RUS_Cargo cargo)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (cargo != null)
                    context.RUS_Cargos.InsertOnSubmit(cargo);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_INSERT_CARGO");
                }
            }
        }

        ///<summary>
        /// Insert
        ///</summary>
        ///<param name="sender"> The sender's id.</param>
        ///<param name="serviceDetailID">The service detail's id.</param>
        ///<param name="count">The count of cargo.</param>
        ///<param name="receiver"> The receive's name.</param>
        ///<param name="total">The total of money.</param>
        ///<param name="paidMoney">The money is paid.</param>
        ///<param name="receivePhone">The receive's phone.</param>
        ///<param name="receiveAddress">The receive's address.</param>
        ///<returns></returns>
        public static RUS_Cargo Insert(int service, int count, int sender, string receiver, string receivePhone, string receiveAddress, string description, decimal total, decimal paidMoney)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (count <= 0)
                    throw new Exception("Số lượng tối thiểu là 1");
                if (CustomerBUS.GetActiveCustomerByID(sender).Customer_ID == 0)
                    throw new Exception("Khách hàng không tồn tại");
                if (String.IsNullOrEmpty(receivePhone))
                    throw new Exception("Điện thoại không được trống");
                if (String.IsNullOrEmpty(receiveAddress))
                    throw new Exception("Địa chỉ không được trống");
                if (String.IsNullOrEmpty(receiver))
                    throw new Exception("Người nhận không được trống");
                RUS_Cargo cargo = new RUS_Cargo();
                cargo.Cargo_serviceDetailID = service;
                cargo.Cargo_count = count;
                cargo.Cargo_sender = sender;
                cargo.Cargo_sendDate = DateTime.Today;
                cargo.Cargo_receiver = receiver;
                cargo.Cargo_receivePhone = receivePhone;
                cargo.Cargo_receiveAddress = receiveAddress;
                cargo.Cargo_description = description;
                cargo.Cargo_total = total;
                cargo.Cargo_paidMoney = paidMoney;
                cargo.Cargo_status = 1;
                cargo.Cargo_isPaid = 0;
                cargo.Cargo_trash = 0;
                context.RUS_Cargos.InsertOnSubmit(cargo);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể thêm đơn hàng mới được.");
                }
                return cargo;
            }
        }

        #endregion

        #region Update
        ///<summary>
        /// Update the cargo
        ///</summary>
        ///<param name="cargo"> The cargo.</param>
        ///<returns></returns>
        public static void Update(RUS_Cargo cargo)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                try
                {
                    if (cargo != null)
                        context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_UPDATE_CARGO");
                }
            }
        }
        
        ///<summary>
        /// Update the specified cargo
        ///</summary>
        ///<param name="id">The id of cargo need updating.</param>
        ///<param name="total"> The total money of cargo.</param>
        ///<param name="payDate">The day pay money.</param>
        ///<returns></returns>
        public static void Update(int id,decimal total)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_ID == id)
                            select c;
                RUS_Cargo cargo = query.SingleOrDefault<RUS_Cargo>();
                if (cargo != null)
                {
                    cargo.Cargo_total = total;
                    cargo.Cargo_isPaid = 1;
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_UPDATE_CARGO");
                }
            }
        }

        ///<summary>
        /// Update the specified cargo
        ///</summary>
        ///<param name="id"> The id of cargo need updating.</param>
        ///<param name="status">The status of cargo</param>
        ///<param name="description">The description of cargo</param>
        ///<returns></returns>
        public static void Update(int id, int status)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_ID == id)
                            select c;
                RUS_Cargo cargo = query.SingleOrDefault<RUS_Cargo>();
                if (cargo != null)
                {
                    cargo.Cargo_status = status;
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_UPDATE_CARGO");
                }
            }
        }

        /// <summary>
        /// Updates the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="service">The service.</param>
        /// <param name="count">The count.</param>
        /// <param name="sender">The sender.</param>
        /// <param name="receiver">The receiver.</param>
        /// <param name="receivePhone">The receive phone.</param>
        /// <param name="receiveAddress">The receive address.</param>
        /// <param name="description">The description.</param>
        /// <param name="total">The total.</param>
        /// <param name="paidMoney">The paid money.</param>
        /// <param name="status">The status.</param>
        /// <param name="isPaid">The is paid.</param>
        /// <param name="trash">The trash.</param>
        /// <returns></returns>
        public static RUS_Cargo Update(int id,int service, int count, int sender, string receiver, string receivePhone, string receiveAddress,
                                       string description, decimal total, decimal paidMoney,int status, int isPaid, int trash)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (count <= 0)
                    throw new Exception("Số lượng tối thiểu là 1");
                if (CustomerBUS.GetActiveCustomerByID(sender).Customer_ID == 0)
                    throw new Exception("Khách hàng không tồn tại");
                if (String.IsNullOrEmpty(receivePhone))
                    throw new Exception("Điện thoại không được trống");
                if (String.IsNullOrEmpty(receiveAddress))
                    throw new Exception("Địa chỉ không được trống");
                if (String.IsNullOrEmpty(receiver))
                    throw new Exception("Người nhận không được trống");
                var query = from car in context.RUS_Cargos
                            where car.Cargo_ID == id
                            select car;
                RUS_Cargo cargo = query.SingleOrDefault<RUS_Cargo>();
                cargo.Cargo_serviceDetailID = service;
                cargo.Cargo_count = count;
                cargo.Cargo_sender = sender;
                cargo.Cargo_receiver = receiver;
                cargo.Cargo_receivePhone = receivePhone;
                cargo.Cargo_receiveAddress = receiveAddress;
                cargo.Cargo_description = description;
                cargo.Cargo_total = total;
                cargo.Cargo_paidMoney = paidMoney;
                cargo.Cargo_status = status;
                cargo.Cargo_isPaid = isPaid;
                cargo.Cargo_trash = trash;
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật đơn hàng được.");
                }
                return cargo;
            }
        }

        #endregion

        #region Delete & Restore
        ///<summary>
        /// Delete the specified cargo.
        ///</summary>
        ///<param name="cargo"> The cargo.</param>
        ///<returns></returns>
        public static RUS_Cargo Delete(RUS_Cargo cargo)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (cargo == null)
                    return null;
                cargo.Cargo_trash = 1;
                try
                {
                    context.RUS_Cargos.Attach(cargo);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGO");
                }
                return cargo;
            }
        }

        ///<summary>
        /// Restore the specified cargo
        ///</summary>
        ///<param name="cargo">The cargo.</param>
        ///<returns></returns>
        public static RUS_Cargo Restore(RUS_Cargo cargo)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (cargo != null)
                    return null;
                cargo.Cargo_trash = 0;
                try
                {
                    context.RUS_Cargos.Attach(cargo);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGO");
                }
                return cargo;
            }
        }

        ///<summary>
        /// Delete the specified cargo by ID
        ///</summary>
        ///<param name="id"> The cargo's id.</param>
        ///<returns></returns>
        public static RUS_Cargo Delete(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_ID == id)
                            select c;
                RUS_Cargo cargo = query.SingleOrDefault<RUS_Cargo>();
                cargo.Cargo_trash = 1;
                try
                {
                    context.RUS_Cargos.Attach(cargo);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGO");
                }
                return cargo;
            }
        }

        ///<summary>
        /// Restores the specified cargo by ID
        ///</summary>
        ///<param name="id"> The cargo's id.</param>
        ///<returns></returns>
        public static RUS_Cargo Restore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (c.Cargo_ID == id)
                            select c;
                RUS_Cargo cargo = query.SingleOrDefault<RUS_Cargo>();
                cargo.Cargo_trash = 0;
                try
                {
                    context.RUS_Cargos.Attach(cargo);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGO");
                }
                return cargo;
            }
        }

        ///<summary>
        /// Delete multiple specified cargos.
        ///</summary>
        ///<param name="ids"> The cargo's ids.</param>
        ///<returns></returns>
        public static List<RUS_Cargo> Delete(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (ids.Contains(c.Cargo_ID))
                            select c;
                List<RUS_Cargo> list = query.ToList<RUS_Cargo>();
                foreach (RUS_Cargo s in list)
                {
                    s.Cargo_trash = 1;
                }
                try
                {
                    context.RUS_Cargos.AttachAll<RUS_Cargo>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGO");
                }
                return list;
            }
        }

        ///<summary>
        /// Restores multiple specified cargos
        ///</summary>
        ///<param name="ids">The ids.</param>
        public static List<RUS_Cargo> Restore(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from c in context.RUS_Cargos
                            where (ids.Contains(c.Cargo_ID))
                            select c;
                List<RUS_Cargo> list = query.ToList<RUS_Cargo>();
                foreach (RUS_Cargo s in list)
                {
                    s.Cargo_ID = 0;
                }
                try
                {
                    context.RUS_Cargos.AttachAll<RUS_Cargo>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGO");
                }
                return list;
            }
        }

        ///<summary>
        ///Deletes multiples specified cargos.
        ///</summary>
        ///<param name="cargos">The cargos</param>
        public static List<RUS_Cargo> Delete(List<RUS_Cargo> cargos)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Cargo c in cargos)
                {
                    c.Cargo_trash = 1;
                }
                try
                {
                    context.RUS_Cargos.AttachAll<RUS_Cargo>(cargos);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_DELETE_CARGO");
                }
                return cargos;
            }
        }

        ///<summary>
        ///Restore multiples specified cargos.
        ///</summary>
        ///<param name="cargos">The cargos</param>
        public static List<RUS_Cargo> Restore(List<RUS_Cargo> cargos)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Cargo c in cargos)
                {
                    c.Cargo_trash = 0;
                }
                try
                {
                    context.RUS_Cargos.AttachAll<RUS_Cargo>(cargos);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("CANNOT_RESTORE_CARGO");
                }
                return cargos;
            }
        }

        ///<summary>
        ///Delete or restore Cargo.
        ///</summary>
        ///<param name="id">The id.</param>
        public static RUS_Cargo DeleteRestore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString)) 
            {
                var query = from cg in context.RUS_Cargos
                            where (cg.Cargo_ID == id)
                            select cg;
                RUS_Cargo cargo = query.SingleOrDefault<RUS_Cargo>();
                if (cargo.Cargo_trash == 0)
                {
                    cargo.Cargo_trash = 1 - cargo.Cargo_trash;
                }
                else
                {
                    cargo.Cargo_trash = 1 - cargo.Cargo_trash;
                }
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Không thể phục hồi tình trạng đơn hàng");
                }
                return cargo;
            }
        }
        #endregion
    }
}
